/*
 *  whoami.c:	test sys_whoami
 *
 *  July 28, 2010
 *  clzqwdy@gmail.com
 */

#define __LIBRARY__		/* 有它, _syscall1等才有效, 详见unistd.h */
#include <unistd.h>		/* 因为_syscall1宏中有系统调用号__NR_whoami的定义 */

#include <stdio.h>		/* printf */
#include <stdlib.h>		/* malloc */
#include <string.h>		/* memset */

#define MAX_NAME_LEN        23
#define NAMEBUF_SIZE        (MAX_NAME_LEN + 1)

_syscall2(int, whoami, char*, name, unsigned int, size)	/* whoami() */

int main(int argc, char* argv[])
{
	int res;
	char* name;
/*	char name[MAX_NAME_LEN];	*/
	unsigned int size;

	if (argc != 1)
		exit(1);
	
	size = MAX_NAME_LEN;
	name = (char*) malloc(NAMEBUF_SIZE);
	if (name == NULL)
		exit(0);

	memset(name, 0, NAMEBUF_SIZE);

	/* need to malloc memory first, or use array */
	/* but array also failed */
	res = whoami(name, size);

	if (res < 0) {
		free(name);
		exit(1);
	} else {
		printf("%s", name);
		free(name);
		exit(0);
	}
}






